import { IPersonalConfig } from '@/api/personalConfig/interface'
import { StateStore } from '@/pages/manager/store'
export type operateType =
	| 'mute' //哑音
	| 'speaker' //发言方
	| 'chairman' //管理方
	| 'doubleFlow' //双流 内容共享
	| 'silence' //静音
	| 'cameraRc' //远遥
	| 'select' //终端选看
	| 'mix' //定制混音
	| 'rec' //终端录像
	| 'oftenMt' //常用终端
	| 'dcs' //参与协作 dcs
	| 'mtVmp' //自主多画面
	| 'upMt' //上传
	| 'forceBrd' //强制广播
	| 'mtInfo' //终端信息
	| 'delete' //删除
	| 'online' //在线、挂断
export type operateMccType = 'mt' | 'subMt' | 'meeting'
type operateOnlineType = Record<operateMccType, operateType[]>
interface IGetOperateOnline {
	config: StateStore
	isSubMeeting: boolean
	isDcs: boolean
	user: {
		enable_dcs: 0 | 1
		enable_video: 0 | 1
	}
}
export type returnTypeOperate = {
	[key in operateMccType]: { icon: operateType[]; dropDown: operateType[] }
}
const OPERATE_ONLINE: operateOnlineType = {
	mt: [
		'mute', //哑音
		'speaker', //发言方
		'chairman', //管理方
		'doubleFlow', //双流 内容共享
		'silence', //静音
		'cameraRc', //远遥
		'select', //终端选看
		'mix', //定制混音
		'rec', //终端录像
		'oftenMt', //常用终端
		'dcs', //参与协作 dcs
		'mtVmp', //自主多画面
		'upMt', //上传
		'forceBrd', //强制广播
		'mtInfo', //终端信息
		'delete', //删除
		'online', //在线、挂断
	],
	subMt: [
		'mute', //哑音
		'speaker', //发言方
		'doubleFlow', //双流 内容共享
		'silence', //静音
		'cameraRc', //远遥
		'mix', //定制混音
		'rec', //终端录像
		'oftenMt', //常用终端
		'dcs', //参与协作 dcs
		'mtVmp', //自主多画面
		'mtInfo', //终端信息
		'delete', //删除
		'online', //在线、挂断
	],
	meeting: [
		'mute', //哑音
		'speaker', //发言方
		'silence', //静音
		'mtInfo', //终端信息
		'delete', //删除
		'online', //在线、挂断
	],
}
function removeOperate(operate: operateOnlineType, ...args: string[]) {
	operate.mt = operate.mt.filter((o) => !args.includes(o))
	operate.subMt = operate.subMt.filter((o) => !args.includes(o))
	operate.meeting = operate.meeting.filter((o) => !args.includes(o))
	return operate
}
export function getOperateOnline({ config, isSubMeeting, isDcs, user }: IGetOperateOnline) {
	let operate = { ...OPERATE_ONLINE }
	const {
		mccInfo: { meetingType, encryptionType, isUpload },
	} = config
	const { enable_dcs, enable_video } = user
	//会议类型
	if (meetingType === 2) {
		//sfu会议 （移除 silence，cameraRc，select，mix，rec，dcs，mtVmp，upMt）
		operate = removeOperate(operate, 'silence', 'cameraRc', 'select', 'mix', 'dcs', 'mtVmp', 'upMt')
	} else {
		//移除 forceBrd
		operate = removeOperate(operate, 'forceBrd')
		if (meetingType === 0) {
			//传统会议（移除 mtVmp）
			operate = removeOperate(operate, 'mtVmp')
		}
	}
	//权限
	if (!enable_video || encryptionType === 3) {
		operate = removeOperate(operate, 'rec')
	}
	//dcs: 不仅有权限控制，也有实时dcs是否开启判断
	if (!(enable_dcs && isDcs)) {
		operate = removeOperate(operate, 'dcs')
	}
	//是否本级会议是级联中的下级节点, 或没权限
	if (!isSubMeeting || !isUpload) {
		operate = removeOperate(operate, 'upMt')
	}
	return operate
}
function transOperate(mt_operate: IPersonalConfig['mt_operate']): Record<operateType, boolean> {
	return {
		cameraRc: !!mt_operate.camera_operation,
		chairman: !!mt_operate.chairman,
		dcs: !!mt_operate.dcs,
		doubleFlow: !!mt_operate.doubleflow,
		forceBrd: !!mt_operate.force_brd,
		mix: !!mt_operate.mix,
		delete: !!mt_operate.mt_delete_online,
		mtInfo: !!mt_operate.mt_info_online,
		online: !!mt_operate.mt_offline,
		rec: !!mt_operate.mt_video,
		mute: !!mt_operate.mute,
		oftenMt: !!mt_operate.often_mt,
		select: !!mt_operate.select,
		silence: !!mt_operate.silence,
		speaker: !!mt_operate.speaker,
		upMt: !!mt_operate.upload,
		mtVmp: !!mt_operate.vmp,
	}
}
export function operateSplitDropDown(operate: operateOnlineType, personalConfig: IPersonalConfig): returnTypeOperate {
	let result = {} as returnTypeOperate
	const { mt_operate } = personalConfig
	let mtOperationVO = transOperate(mt_operate)
	for (const [key, value] of Object.entries(operate)) {
		let icon = [] as operateType[],
			dropDown = [] as operateType[]
		for (const o of value) {
			if (mtOperationVO[o]) {
				icon.push(o)
			} else {
				dropDown.push(o)
			}
		}
		result[key as operateMccType] = { icon, dropDown }
	}
	return result
}
